// parking_hardware.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "parking_hardware.h"

// 保持原有的全局变量
static int buzzer_fd = -1;
static int led_fd = -1;

// 保持原有的init_buzzer_device函数
int init_buzzer_device(void) {
    buzzer_fd = open("/dev/beep", O_RDWR);
    if(buzzer_fd < 0) {
        printf("ERROR: Failed to open buzzer device /dev/beep, errno=%d\n", errno);
        perror("open error");
        return -1;
    }
    printf("SUCCESS: Buzzer device initialized successfully, fd=%d\n", buzzer_fd);
    return 0;
}

// 保持原有的init_led_device函数
int init_led_device(void) {
    printf("Attempting to open LED device...\n");

    if(access("/dev/Led", F_OK) != 0) {
        printf("ERROR: LED device file /dev/Led does not exist\n");
        return -1;
    }

    led_fd = open("/dev/Led", O_RDWR);
    if(led_fd < 0) {
        printf("ERROR: Failed to open LED device, errno=%d\n", errno);
        perror("open error");
        return -1;
    }

    printf("SUCCESS: LED device opened, fd=%d\n", led_fd);

    printf("Testing LED device functionality...\n");
    if(ioctl(led_fd, LED1, LED_ON) < 0) {
        printf("WARNING: LED device test failed, but continuing...\n");
        perror("ioctl test error");
    } else {
        ioctl(led_fd, LED1, LED_OFF);
        printf("LED device test passed\n");
    }

    return 0;
}

// 保持原有的control_led函数
void control_led(int led_num, int state) {
    if(led_fd < 0) {
        printf("LED device not initialized\n");
        return;
    }

    switch(led_num) {
        case 1: ioctl(led_fd, LED1, state); break;
        case 2: ioctl(led_fd, LED2, state); break;
        case 3: ioctl(led_fd, LED3, state); break;
        case 4: ioctl(led_fd, LED4, state); break;
        default: printf("Invalid LED number: %d\n", led_num); return;
    }

    printf("LED %d set to %s\n", led_num, state == LED_ON ? "ON" : "OFF");
}

// 保持原有的control_all_leds函数
void control_all_leds(int state) {
    for(int i = 1; i <= 4; i++) {
        control_led(i, state);
    }
    printf("All LEDs set to %s\n", state == LED_ON ? "ON" : "OFF");
}

// 保持原有的blink_leds函数
void blink_leds(int times, int delay_ms) {
    for(int i = 0; i < times; i++) {
        control_all_leds(LED_ON);
        usleep(delay_ms * 1000);
        control_all_leds(LED_OFF);
        usleep(delay_ms * 1000);
    }
    printf("LEDs blinked %d times\n", times);
}

// 保持原有的control_buzzer函数
void control_buzzer(int state) {
    if(buzzer_fd < 0) {
        printf("ERROR: Buzzer device not initialized, fd=%d\n", buzzer_fd);
        return;
    }

    printf("DEBUG: Controlling buzzer, state: %s, fd=%d\n", state == BUZZER_ON ? "ON" : "OFF", buzzer_fd);

    int result = ioctl(buzzer_fd, state, 1);
    if(result < 0) {
        printf("ERROR: Failed to control buzzer, ioctl returned %d, errno=%d\n", result, errno);
        perror("ioctl error");
    } else {
        printf("SUCCESS: Buzzer set to %s\n", state == BUZZER_ON ? "ON" : "OFF");
    }
}

// 保持原有的buzzer_alarm函数
void buzzer_alarm(int times, int delay_ms) {
    printf("Starting buzzer alarm: times=%d, delay=%dms\n", times, delay_ms);

    for(int i = 0; i < times; i++) {
        control_buzzer(BUZZER_ON);
        usleep(delay_ms * 1000);
        control_buzzer(BUZZER_OFF);
        if(i < times - 1) {
            usleep(delay_ms * 1000);
        }
    }
    printf("Buzzer alarm finished\n");
}

// 保持原有的buzzer_continuous_alarm函数
void buzzer_continuous_alarm(int duration_sec) {
    printf("Starting continuous buzzer alarm: duration=%ds\n", duration_sec);

    control_buzzer(BUZZER_ON);
    sleep(duration_sec);
    control_buzzer(BUZZER_OFF);

    printf("Continuous buzzer alarm finished\n");
}

// 关闭硬件设备
void close_hardware_devices(void) {
    if(buzzer_fd >= 0) {
        close(buzzer_fd);
        printf("Buzzer device closed\n");
    }
    
    if(led_fd >= 0) {
        close(led_fd);
        printf("LED device closed\n");
    }
}